//
//  UIViewController+ShowAlertController.m
//  UnionParking
//
//  Created by Fun Tech on 2018/1/19.
//  Copyright © 2018年 Union. All rights reserved.
//

#import "UIViewController+ShowAlertController.h"

@implementation UIViewController (ShowAlertController)

- (void)j_appearance{
    if (@available(iOS 13.0, *)) {
        [self setOverrideUserInterfaceStyle:UIUserInterfaceStyleLight];
    } else {
        // Fallback on earlier versions
    }
    if (@available(iOS 15, *)) {
         UINavigationBarAppearance * appearance = [[UINavigationBarAppearance alloc] init];
         [appearance configureWithOpaqueBackground];
         // 背景色
         appearance.backgroundColor = [UIColor whiteColor];

         // 去除导航栏阴影（如果不设置clear，导航栏底下会有一条阴影线）
//         appearance.shadowColor = [UIColor clearColor];
         // 字体颜色
         appearance.titleTextAttributes = @{
             NSForegroundColorAttributeName: J_ColorFromHEX(0x000000),
             NSFontAttributeName:J_MediumFont(18)
         };

         // 带scroll滑动的页面
         UINavigationBar.appearance.scrollEdgeAppearance = appearance;
         // 常规页面
         UINavigationBar.appearance.standardAppearance = appearance;
         
         UITableView.appearance.sectionHeaderTopPadding = 0.f;
         
         UIColor *color = [UIColor whiteColor]; // #F5F5F5 for white smoke.
         UITabBarAppearance *tabBarAppearance = [UITabBarAppearance new];
         tabBarAppearance.backgroundColor = color;
         [[UITabBar appearance] setStandardAppearance:tabBarAppearance];
         [[UITabBar appearance] setScrollEdgeAppearance:tabBarAppearance];
     }
}

#pragma mark--alert
// 单个按钮提示，按钮不做任何动作
- (void)lq_showAlertControllerWithMessage:(NSString *)message{
    
    UIAlertController *alertView = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"提示", @"")
                                                                       message:message
                                                                preferredStyle:UIAlertControllerStyleAlert ];
    
    UIAlertAction *OKAction = [UIAlertAction actionWithTitle:FYLocalizedString(@"oxygen_top_determine", @"") style:UIAlertActionStyleDefault handler:nil];
    [alertView addAction:OKAction];
    
    [self presentViewController:alertView animated:YES completion:nil];;
}

// 单个按钮提示，有确定，有动作
- (void)lq_showAlertControllerWithMessage:(NSString *)message
                    okHandle:(dispatch_block_t)okHandle{
    
    UIAlertController *alertView = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"提示", @"")
                                                                       message:message
                                                                preferredStyle:UIAlertControllerStyleAlert ];
    
    UIAlertAction *OKAction = [UIAlertAction actionWithTitle:FYLocalizedString(@"oxygen_top_determine", @"") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        if (okHandle) okHandle();
        
    }];
    [alertView addAction:OKAction];
    
    [self presentViewController:alertView animated:YES completion:nil];
}

// 双按钮提示，有确定，取消，都有动作
- (void)lq_showAlertControllerWithMessage:(NSString *)message
                cancleHandle:(dispatch_block_t)cancleHandle
                    okHandle:(dispatch_block_t)okHandle{
    UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@""
                                                                       message:message
                                                                preferredStyle:UIAlertControllerStyleAlert ];
    
    UIAlertAction *OKAction = [UIAlertAction actionWithTitle:FYLocalizedString(@"oxygen_top_determine", @"") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        if (okHandle) okHandle();
        
    }];
    [OKAction setValue:J_ColorFromHEX(0x0095B6) forKey:@"_titleTextColor"];
    [alertView addAction:OKAction];
    
    UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:FYLocalizedString(@"oxygen_top_cancel", @"") style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
        if (cancleHandle) cancleHandle();
        
    }];
    [alertView addAction:cancleAction];
    [cancleAction setValue:J_ColorFromHEX(0x666666) forKey:@"_titleTextColor"];
    
    [self presentViewController:alertView animated:YES completion:nil];
}

#pragma mark--sheet
// alert sheet
- (void)lq_showAlertSheetControllerWithTitles:(NSArray *)titles
                                   handles:(dispatch_block_t)fristHandles,...NS_REQUIRES_NIL_TERMINATION{
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil
                                                                             message:nil
                                                                      preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:FYLocalizedString(@"oxygen_top_cancel", @"") style:UIAlertActionStyleCancel handler:nil];
    [alertController addAction:cancelAction];
    [cancelAction setValue:[UIColor redColor] forKey:@"_titleTextColor"];// 修改取消的颜色
    
    int i = 0;
    va_list args;
    va_start(args, fristHandles);
    for (dispatch_block_t handle = fristHandles; handle != nil; handle = va_arg(args,dispatch_block_t)) {
        
        UIAlertAction *action = [UIAlertAction actionWithTitle:titles[i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            if (handle) handle();
            
        }];
        [alertController addAction:action];
        i++;
    }
    va_end(args);
    [self presentViewController:alertController animated:YES completion:nil];
}

- (void)lq_showAlertSheetControllerWithTitles:(NSArray *)titles
                            CompleteHandle:(void(^)(NSString *flag))handles{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil
                                                                             message:nil
                                                                      preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:FYLocalizedString(@"oxygen_top_cancel", @"") style:UIAlertActionStyleCancel handler:nil];
    [alertController addAction:cancelAction];
    [cancelAction setValue:[UIColor redColor] forKey:@"_titleTextColor"];// 修改取消的颜色
    
    for (NSString *title in titles) {
        
        UIAlertAction *action = [UIAlertAction actionWithTitle:title style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            if (handles){
                handles(title);
            }
            
        }];
        [alertController addAction:action];
    }
    [self presentViewController:alertController animated:YES completion:nil];
}

#pragma mark--edit text

- (void)lq_showEditAlertWithTitle:(NSString *)title
               WithPlaceholder:(NSArray *)placeholders
                CompleteHandle:(void(^)(NSString *result))handle;{

    UIAlertController *alertView = [UIAlertController alertControllerWithTitle:NSLocalizedString(title, @"")
                                                                       message:nil
                                                                preferredStyle:UIAlertControllerStyleAlert ];
    
    for (NSString *placeholder in placeholders) {
        [alertView addTextFieldWithConfigurationHandler:^(UITextField *textField){
            textField.clearButtonMode = UITextFieldViewModeAlways;
            textField.placeholder = placeholder;
            textField.textAlignment = NSTextAlignmentCenter;
//            textField.jk_maxLength = 10;
        }];
    }
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alertTextFieldDidChange:) name:UITextFieldTextDidChangeNotification object:nil];
    
    UIAlertAction *OKAction = [UIAlertAction actionWithTitle:FYLocalizedString(@"oxygen_top_determine", @"") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
        UITextField *nameTF = alertView.textFields.firstObject;
        if (handle) {
            handle(nameTF.text);
        }
    }];
    OKAction.enabled = NO;
    [OKAction setValue:J_ColorFromHEX(0x0095B6) forKey:@"_titleTextColor"];
    [alertView addAction:OKAction];
    
    UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:FYLocalizedString(@"oxygen_top_cancel", @"") style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
    }];
    [cancleAction setValue:J_ColorFromHEX(0x666666) forKey:@"_titleTextColor"];// 修改取消的颜色
    [alertView addAction:cancleAction];
    
    [self presentViewController:alertView animated:YES completion:nil];
    
}

- (void)alertTextFieldDidChange:(NSNotification *)sender{
    
    UIAlertController *alertController = (UIAlertController *)self.presentedViewController;
    if (alertController) {
        //UITextField *nameTF = alertController.textFields.firstObject;
        NSInteger count = 0;
        for (UITextField *tempTF in alertController.textFields) {
            if (tempTF.text.length > 0) {
                count += 1;
            }
        }
        UIAlertAction *okAction = alertController.actions.firstObject;
        okAction.enabled = count == alertController.textFields.count;
    }
}


@end


#pragma mark--处理递归调用bug
@interface UIAlertController (supportedInterfaceOrientations)

@end

@implementation UIAlertController (supportedInterfaceOrientations)

#if __IPHONE_OS_VERSION_MAX_ALLOWED < 90000
- (NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait;
}
#else
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}
#endif

@end
